package net.ib.mtalk.network.task;

import net.ib.mntalk.protocol.Common;
import net.ib.mntalk.protocol.Session.UnRegisterAccountReq;
import net.ib.mntalk.protocol.Session.UnRegisterAccountRes;
import net.ib.mntalk.protocol.Session.UnRegisterUserRes;
import net.ib.mtalk.constant.HandlerMsgConst;
import net.ib.mtalk.network.TcpEnvelope;
import net.ib.mtalk.network.MethodId.Session;
import net.ib.mtalk.util.MTalkLogWriter;
import android.content.Context;
import android.os.Handler;

public class UnRegisterAccountTask extends AsyncNetTask{
	private UnRegisterAccountReq mReq;
	
	public UnRegisterAccountTask(Context context, Handler handler,UnRegisterAccountReq req) {
		super(context, handler);
		mReq = req;
	}

	protected TcpEnvelope onPreExecute() {
		TcpEnvelope revelop = new TcpEnvelope.Builder()
		.setMethodId(Session.Registration.UnRegisterAccount)
		.setMessage(mReq)
		.setType(TcpEnvelope.REQUEST)
		.build();
		MTalkLogWriter.d("UnRegisterAccountTask.onPreExecute()");
		return revelop;
	}
	
	protected void onPostExecute(int resultCode, Object sentObject,
			Object receivedObject) {
		MTalkLogWriter.d("UnRegisterAccountTask.onPostExecute() resultCode : " + resultCode);
		if (receivedObject == null) {
			mUiHandler.sendMessageAtFrontOfQueue(mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_UNKNOWN_ERROR));
			return;
		}
		
		TcpEnvelope resultEnvelope = (TcpEnvelope) receivedObject;
		UnRegisterAccountRes unRegisterAccountRes = (UnRegisterAccountRes) resultEnvelope.getMessage();
		
		MTalkLogWriter.d("UnRegisterUserTask ResponseCode : " + unRegisterAccountRes.getResponseCode());
		
		if(unRegisterAccountRes.getResponseCode() == Common.SUCCESS){
			mUiHandler.sendMessageAtFrontOfQueue(mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_UNREGISTER_ACCOUNT_SUCCESS));
		} else{
			mUiHandler.sendMessageAtFrontOfQueue(mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_UNREGISTER_ACCOUNT_FAIL));
		}
	}


}
